<template>
  <div id="sub-items-echarts" :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from '../mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '260px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      //修改图表高度
      this.height = (document.getElementsByClassName('day')[0].offsetHeight - 30 ) + 'px';

      this.chart = echarts.init(this.$el, 'macarons');
      this.setOptions(this.chartData);

      window.addEventListener('resize', () =>{
        if (this.chart) {
          let fatherHeight = document.getElementsByClassName('day')[0].offsetHeight - 30
          this.height = fatherHeight + 'px';
          this.chart.resize();
        }
      })
    },
    setOptions({ ngData, okData } = {}) {
      this.chart.setOption({
        color:['#488aff','#42b983'],
        grid:{
          show:false,
          top:'30px',
          bottom:'20px',
          left:'5%',
          right:'2%'
        },
        legend: {
          data: ['产量（台）[Output（PCS）]', ' '],
          left:'5%',
          textStyle:{
            color: '#FFF'
          }
        },
        xAxis: [
          {
            type: 'category',
            data: ['0', '01', '02', '03', '04', '05', '06','07', '08', '09', '10', '11', '12', '13', '14', '15','16', '17', '18', '19', '20', '21', '22', '23'],
            axisPointer: {
              type: 'shadow'
            },
            axisLine:{
              show: true,
              lineStyle:{
                color: '#FFF'
              }
            }
          }
        ],
        yAxis: [
          {
            type: 'value',
            axisLabel: {
              formatter: '{value}'
            },
            splitLine: {
              show : false
            },
            axisLine:{
              show: true,
              lineStyle:{
                color: '#FFF'
              }
            }
          }
        ],
        series: [
          {
            name: '产量（台）[Output（PCS）]',
            type: 'bar',
            label: {
              show: true,
              position: 'top',
              color: '#FFF'
            },
            data: [
              2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3,
              2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3
            ]
          },
          {
            name: '123',
            type: 'bar',
            label: {
              show: true,
              position: 'top',
              color: '#FFF'
            },
            data: [
              2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3,
              2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3
            ]
          },
          {
            name: ' ',
            type: 'line',
            data: [
              2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3,
              2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3
            ]
          }
        ]
      })
    }
  }
}
</script>
